<template>
	<view class="login">
		<nav-bar></nav-bar>
		<view class="title">您好!</view>
		<view class="subtitle" v-if="form.type == 1">欢迎使用奥莱门店接待端</view>
		<view class="subtitle" v-else>欢迎使用奥莱门店管理端</view>
		<view class="form">
			<input placeholder="请输入账号" type="text" v-model="form.userName" />
			<input placeholder="请输入密码" password v-model="form.password" />
		</view>
		<view class="confirm" @click="login">立即登录</view>
	</view>
</template>

<script>
	import {
		storeLoginApi
	} from "@/api/store.js"
	export default {
		data() {
			return {
				form: {
					userName: '',
					password: '',
					type: 1
				}
			}
		},
		onLoad(opt) {
			this.form.type = opt.type || 1
			// 用户已经登录过了
			if (this.$utils.storage.get('store_token')) {
				this.$Router.replace('/pages/store/menus/menus')
			}
		},
		methods: {
			async login() {
				if (!this.form.userName) return this.$utils.toast('账号不能为空')
				if (!this.form.password) return this.$utils.toast('密码不能为空')
				let {
					data
				} = await storeLoginApi({
					...this.form,
					user_name: this.form.userName
				})
				this.$utils.storage.set('store_token', data.store_token)
				this.$utils.success('登录成功')
				setTimeout(() => {
					this.$Router.push(`/pages/store/menus/menus?type=${this.form.type}`)
				}, 1000)
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'index.scss';
</style>
